
#include "hal_stm.h"

static STM_CH_Config stm_config[STM_CHANNELS] ={
    {FALSE, 0, NULL},
    {FALSE, 0, NULL},
    {FALSE, 0, NULL},
    {FALSE, 0, NULL},
};
void hal_stm_init(stm_channel_en channel ,uint32_t delay_us ,stmcallback_t stmcbk , enabled_en en)
{
	STMD.stm_ch_config = stm_config;
	if(HAL_ENABLE == en)
	{
		stm_config[channel].enabled = TRUE;
		stm_config[channel].callback = stmcbk;
		stm_config[channel].counter = delay_us;
	}
	else if(HAL_DISABLE == en)
	{
		stm_config[channel].enabled = FALSE;
		stm_config[channel].callback = stmcbk;
		stm_config[channel].counter = 0;
	}
	else
	{
		
	}
	hal_stm_iqr_config(channel,PSR_PRI_10,en);
	STM_LLD_Configure(&STMD);				
	STM_LLD_ModuleEnable(&STMD);
	STM_LLD_SetPrescaler(HAL_SYS_CLOCK_MHZ);
	STM_LLD_SetCompareValue(channel, delay_us);
}
void hal_stm_enabled(stm_channel_en channel ,enabled_en en)
{
#if (STM_USE_STM0 == TRUE)
    //STM_LLD_DeInit(&STMD);
#endif
    if(HAL_DISABLE == en)
    {
    	STMD.stm_ch_config[channel].enabled = TRUE;
    }
    else if(HAL_ENABLE == en)
    {
    	STMD.stm_ch_config[channel].enabled = FALSE;
    }
    else
    {
    	
    }
    STM_LLD_ModuleEnable(&STMD);
}
void hal_stm_cbk(stm_channel_en channel ,stmcallback_t stmcbk )
{
	STMD.stm_ch_config[channel].callback = stmcbk;
}
void hal_stm_iqr_config(stm_channel_en channel ,INTC_PSR_PRI_t priorty , enabled_en en)
{
#if (STM_USE_STM0 == TRUE)
    //STM_LLD_DeInit(&STMD);
#endif
    switch(channel)
    {
    case STM_CHANNEL0:
    	INTC_InstallINTCInterruptHandler(stm_config[0].callback ,STM0_CH0_INT_NUMBER, priorty);
    	break;
    case STM_CHANNEL1:
    	INTC_InstallINTCInterruptHandler(stm_config[1].callback ,STM0_CH1_INT_NUMBER, priorty);
    	break;
    case STM_CHANNEL2:
    	INTC_InstallINTCInterruptHandler(stm_config[2].callback ,STM0_CH2_INT_NUMBER, priorty);
    	break;
    case STM_CHANNEL3:
    	INTC_InstallINTCInterruptHandler(stm_config[3].callback ,STM0_CH3_INT_NUMBER, priorty);
    	break;
    default:
    	break;
    }   
    
    if(HAL_DISABLE == en)
    {
    	STMD.stm_ch_config[channel].enabled = FALSE;
    }
    else if(HAL_ENABLE == en)
    {
    	STMD.stm_ch_config[channel].enabled = TRUE;
    }
    else
    {
    	
    }
}

void hal_stm_resetcount(stm_channel_en channel)
{
	//STM_LLD_ResetCounter(&STMD);
	STMD.stm_tagp->CH[channel].CIR.B.CIF = STM_CCR_CIF_CLEAR;
}
